package com.yms.socket.client;


import com.yms.socket.SelectorUtil;
import com.yms.socket.server.DatagramChannelServer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @version : 1.0
 * @author: yangmingsong
 * @date: 2022/7/20-07-20-16:37
 * @description: spring5Test
 */
public class DatagramChannelClient {
    private static Lock lock = new ReentrantLock();
    private static Condition readLock = lock.newCondition();
    private static Condition writeLock = lock.newCondition();
    private static Selector selector = SelectorUtil.getSelector();
    private  static DatagramChannel datagramChannel = null;
    private  static  ByteBuffer buffer = ByteBuffer.allocate(1024);
    static {

        try {
            datagramChannel = DatagramChannel.open();
            datagramChannel.configureBlocking(false);

            datagramChannel.register(selector, SelectionKey.OP_READ);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) throws IOException {
        try{
            Scanner scanner= new Scanner(System.in);
            while (scanner.hasNext()){
                String s = scanner.next();
                buffer.put(s.getBytes());
                buffer.flip();
//            反转为可读
                datagramChannel.send(buffer,new InetSocketAddress("127.0.0.1",8088));
//            反转为可写
                buffer.clear();
            }
        }catch (Exception e){
            e.getMessage();
        }

    }



}

